<template>
  <div class="activity-topic-showcase">
    <div class="flex justify-between mb-2">
      <el-button type="primary" @click="openDialog">选择活动主题</el-button>
      <el-button v-if="modelValue.length > 0" @click="clearSelection">清空</el-button>
    </div>
    <div v-if="modelValue.length === 0" class="text-gray-400 text-center py-4">
      暂无选择活动主题
    </div>
    <el-table v-else :data="topicList" :stripe="true" class="mb-4">
      <el-table-column label="活动名称" prop="name" min-width="200" show-overflow-tooltip />
      <el-table-column label="开始时间" prop="startTime" min-width="160">
        <template #default="{ row }">
          {{ formatDate(row.startTime, 'YYYY-MM-DD') }}
        </template>
      </el-table-column>
      <el-table-column label="结束时间" prop="endTime" min-width="160">
        <template #default="{ row }">
          {{ formatDate(row.endTime, 'YYYY-MM-DD') }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="80" align="center">
        <template #default>
          <el-button
            type="primary"
            circle
            size="small"
            @click="clearSelection"
          >
            <Icon icon="ep:delete" />
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 活动主题选择弹窗 -->
    <el-dialog
      v-model="dialogVisible"
      title="选择活动主题"
      width="60%"
      destroy-on-close
      append-to-body
    >
      <el-form :model="queryParams" ref="queryFormRef" :inline="true">
        <el-form-item label="活动名称" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入活动名称"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <Icon icon="ep:search" class="mr-5px" />
            搜索
          </el-button>
          <el-button @click="resetQuery">
            <Icon icon="ep:refresh" class="mr-5px" />
            重置
          </el-button>
        </el-form-item>
      </el-form>

      <el-table
        v-loading="loading"
        :data="list"
        @row-click="handleRowClick"
        ref="tableRef"
        highlight-current-row
      >
        <el-table-column label="活动名称" prop="name" min-width="200" show-overflow-tooltip />
        <el-table-column label="开始时间" prop="startTime" min-width="160">
          <template #default="{ row }">
            {{ formatDate(row.startTime, 'YYYY-MM-DD') }}
          </template>
        </el-table-column>
        <el-table-column label="结束时间" prop="endTime" min-width="160">
          <template #default="{ row }">
            {{ formatDate(row.endTime, 'YYYY-MM-DD') }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="100" align="center">
          <template #default="{ row }">
            <el-button
              type="primary"
              link
              @click="handleSelect(row)"
            >
              选择
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-if="total > 0"
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleConfirm">确 定</el-button>
          <el-button @click="dialogVisible = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { ActivityTopicApi, ActivityTopicVO } from '@/api/mall/promotion/activityTopic'
import { formatDate } from '@/utils/formatTime'
import { ElMessage } from 'element-plus'

const props = defineProps<{
  modelValue: number[]
}>()

const emit = defineEmits(['update:modelValue'])

// 活动主题列表
const topicList = ref<ActivityTopicVO[]>([])
// 弹窗是否显示
const dialogVisible = ref(false)
// 加载状态
const loading = ref(false)
// 总条数
const total = ref(0)
// 列表数据
const list = ref<ActivityTopicVO[]>([])
// 选中的数据
const selectedRow = ref<ActivityTopicVO | null>(null)

// 查询参数
const queryParams = ref({
  pageNo: 1,
  pageSize: 10,
  name: ''
})

// 查询列表
const getList = async () => {
  loading.value = true
  try {
    const res = await ActivityTopicApi.getActivityTopicPage(queryParams.value)
    list.value = res.list
    total.value = res.total
  } finally {
    loading.value = false
  }
}

// 搜索按钮操作
const handleQuery = () => {
  queryParams.value.pageNo = 1
  getList()
}

// 重置按钮操作
const resetQuery = () => {
  queryParams.value = {
    pageNo: 1,
    pageSize: 10,
    name: ''
  }
  handleQuery()
}

// 打开弹窗
const openDialog = () => {
  dialogVisible.value = true
  getList()
  selectedRow.value = null
}

// 清空选择
const clearSelection = () => {
  emit('update:modelValue', [])
  topicList.value = []
}

// 行点击事件
const handleRowClick = (row: ActivityTopicVO) => {
  selectedRow.value = row
}

// 选择按钮点击
const handleSelect = (row: ActivityTopicVO) => {
  selectedRow.value = row
  handleConfirm()
}

// 确认选择
const handleConfirm = () => {
  if (!selectedRow.value) {
    ElMessage.warning('请选择活动主题')
    return
  }
  emit('update:modelValue', [selectedRow.value.id])
  topicList.value = [selectedRow.value]
  dialogVisible.value = false
}

// 监听 modelValue 变化，更新活动主题列表
watch(
  () => props.modelValue,
  async (newIds) => {
    if (newIds && newIds.length > 0) {
      const topic = await ActivityTopicApi.getActivityTopicById(newIds[0])
      topicList.value = [topic]
    } else {
      topicList.value = []
    }
  },
  {
    immediate: true
  }
)

const tableRef = ref()
</script>

<style scoped lang="scss">
.activity-topic-showcase {
  :deep(.el-table) {
    margin-top: 8px;
  }
}
</style> 